﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.IO;
namespace Guline.Web.JXAccount.Controllers
{
       [Authorize(Users = "gulinejxad,sdatavnadm, huyhieu19911")]
    public class CardController : BaseController
    {
        //
        // GET: /Card/

        private Models.Caderer Cardmn;
        public ActionResult Add()
        {
            return View();
        }
        public ActionResult LogTrue(string ngay)
        {
            string thedung, thesai;
            if (string.IsNullOrEmpty(ngay))
            {
                ngay = DateTime.Now.ToString("yyyy-MM-dd");                
            }
            else
            {
                try
                {
                    ngay = DateTime.Parse(ngay).ToString("yyyy-MM-dd");

                }
                catch
                {
                    ngay = DateTime.Now.ToString("yyyy-MM-dd");  
                }
            }
            try
            {
                thedung = System.IO.File.ReadAllText(Server.MapPath("~/CardCharge/true-" + ngay + ".pop")).Replace("\r\n","<br/>");
            }
            catch
            {
                thedung = "";

            }
            try
            {
                thesai = System.IO.File.ReadAllText(Server.MapPath("~/CardCharge/false-" + ngay + ".pop")).Replace("\r\n","<br/>");
            }
            catch
            {
                thesai = "";

            }
            ViewBag.thedung = thedung;
            ViewBag.thesai = thesai;
            ViewBag.ngay = ngay;
            return View();
        }
        public ActionResult Index()
        {
            Cardmn = new Models.Caderer(Server.MapPath("~/bin/GulineJX.pop"));
            List<Guline.Web.JXAccount.Models.CardCharge> ListCad = this.Cardmn.getList();
            return View(ListCad);
        }
        [HttpPost]
        public ActionResult Add(Models.CardCharge model)
        {
            Cardmn = new Models.Caderer(Server.MapPath("~/bin/GulineJX.pop"));
            if (ModelState.IsValid)
            {
                if (Cardmn.Add(model))
                {
                    Cardmn.Save();
                    return RedirectToAction("Index");
                }
                else

                {
                    ModelState.AddModelError("", "Đã tồn tại tỉ lệ này.");
                }
               
            }
            return View(model);
        }
        public ActionResult Delete(string money)
        {
            Cardmn = new Models.Caderer(Server.MapPath("~/bin/GulineJX.pop"));
            Cardmn.Delete(money);
            Cardmn.Save();
            return RedirectToAction("Index");
        }
        public ActionResult Edit(string money)
        {
            Cardmn = new Models.Caderer(Server.MapPath("~/bin/GulineJX.pop"));
            return View(Cardmn.Search(money));
        }
        [HttpPost]
        public ActionResult Edit(Models.CardCharge model)
        {

            Cardmn = new Models.Caderer(Server.MapPath("~/bin/GulineJX.pop"));
            if (ModelState.IsValid)
            {
                if (Cardmn.Edit(model))
                {
                    Cardmn.Save();
                    return RedirectToAction("Index");
                }
                else
                {
                    ModelState.AddModelError("", "Đã tồn tại tỉ lệ này.");
                }

            }
            return View(model);
        }
        
        
    }
}
